package com.ajie.controller;


import com.ajie.entity.Course;
import com.ajie.service.CourseService;
import com.ajie.utils.DateUtils;
import com.ajie.utils.QueryInfo;
import com.ajie.utils.Result;
import com.ajie.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 课程数据控制层
 */
@RestController
@Api(tags = "课程数据")
@RequestMapping("/course")
@Slf4j
public class CourseController {

    @Autowired
    private CourseService courseService;
    
    @Autowired
    private HttpServletRequest request;
    
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;


    @ApiOperation(value = "分页查询")
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryInfo queryInfo){
        log.info("该文件名是：CourseController,该方法是：findPage");
        String header = request.getHeader(tokenHeader);
        return courseService.findPage(queryInfo,header);
    }
    
    @ApiOperation(value = "分页查询")
    @PostMapping("/findPageCheck")
    public Result findPageCheck(@RequestBody QueryInfo queryInfo){
        log.info("该文件名是：CourseController,该方法是：findPage");
        String header = request.getHeader(tokenHeader);
        return courseService.findPageCheck(queryInfo,header);
    }
    @ApiOperation(value = "添加课程")
    @PostMapping("/insert")
    public Result insert(@RequestBody Course course){
        log.info("该文件名是：CourseController,该方法是：insert");
        log.info("course111{}",course);
        course.setCreateUid(SecurityUtil.getUserId());
        course.setCreateTime(DateUtils.getDateTime());
        course.setDel(false);
        return courseService.insert(course);
    }

    @ApiOperation(value = "删除课程")
    @DeleteMapping("/delete/{id}")
    public Result delete( @PathVariable("id") Long id){
        log.info("该文件名是：CourseController,该方法是：delete");
        return courseService.delete(id);
    }

    @ApiOperation(value = "修改课程")
    @PutMapping("/update")
    public Result update(@RequestBody Course course){
        log.info("该文件名是：CourseController,该方法是：update");
        return courseService.update(course);
    }
    
    @ApiOperation(value = "获取详细课程")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") Long id){
        log.info("该文件名是：CourseController,该方法是：getInfo");
        return courseService.getInfo(id);
    }
    
    @ApiOperation(value = "修改评论状态")
    @PutMapping("/updateStatus/{id}")
    public Result updateStatus(@PathVariable("id") Long id){
        log.info("该文件名是：NewController,该方法是：update");
        return courseService.updateStatus(id);
    }
}
